package com.group3.group3.post.service.impl;
/**
 * Created with IntelliJ IDEA.
 *
 * @Author: Tom
 * @Date: 2022/05/19/22:37
 * @Description:
 */

/**
 *@ClassName PostServiceImpl
 *@Description
 *@Author 王琛
 *@Date 2022/5/19 22:37
 @Version 1.0
 */

import com.group3.group3.mybatis.entity.PtDutyEntity;
import com.group3.group3.mybatis.flexigrid.PadingRstType;
import com.group3.group3.mybatis.flexigrid.PagingBean;
import com.group3.group3.mybatis.ztree.ZtreeEntity;
import com.group3.group3.post.entity.SearchPostEntity;
import com.group3.group3.post.repository.PostDao;
import com.group3.group3.post.service.PostService;
import com.group3.group3.role.repository.RoleDao;
import com.group3.group3.spring.util.DataUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Locale;

@Service("postService")
public class PostServiceimpl implements PostService {
    @Resource
    private RoleDao roleDao;
    @Value("${ubound.uuid}")
    private String uboundUuid;
    @Resource
    private MessageSource messageSource;
    @Override
    public List<ZtreeEntity> getPostTree() {

        ZtreeEntity ZtreeEntity = new ZtreeEntity();
        ZtreeEntity.setId(uboundUuid);
        Locale locale = LocaleContextHolder.getLocale();
        List<ZtreeEntity> list = roleDao.getPostZtreeList(locale.toString());
        String unboundName = messageSource.getMessage("common.unbound.name",null,locale);
        ZtreeEntity.setName(unboundName);
        list.add(ZtreeEntity);
        Integer index = 0;
        for(ZtreeEntity item:list){
            if(item!=null){
                if(index < 10){
                    item.setOpen(true);
                }else{
                    item.setOpen(false);
                }
                index++;
            }
        }

        return list;
    }

    @Resource
    private PostDao postDao;

    @Override
    public PtDutyEntity getPostInfoById(String postId) {
        return postDao.getPostInfoById(postId);
    }

    @Override
    public PadingRstType<PtDutyEntity> getPostInfoListByPage(SearchPostEntity search, PagingBean paging) {
        paging.deal(PtDutyEntity.class);
        PadingRstType<PtDutyEntity> padingRstType = new PadingRstType<>();
        padingRstType.setPage(paging.getPage());

        List<PtDutyEntity> list=postDao.queryPostListByPage(search,paging);
        Integer total=postDao.queryPostListTotal(search);
        padingRstType.setRawRecords(list);
        padingRstType.setTotal(total);

        padingRstType.putItems();
        return padingRstType;
    }

    @Override
    public void deletePostById(String postId) {
        postDao.deletePostById(postId);
    }

    @Override
    public void modifyPostInfoById(PtDutyEntity entity) {
        postDao.modifyPostInfoById(entity);

    }

}
